#!/bin/bash
# vim: fdm=marker fmr=[[[,]]] cms=\ #%s

[[ -z $SOAR ]] && SOAR=/opt/soar
PATH=$PATH:$SOAR/common/bash/soar
. soar.sh

main()
{ #[[[
    if [[ -z $SVCENV_CNFG ]]
    then
        echo "Error: SVCENV_CNFG not set"
        return 1
    fi
    local bin=seagent

    local ipckey=$(cnfg_get_property "$SVCENV_CNFG" GLOBAL IPCKEY)
    if [[ -z $ipckey ]]
    then
        echo "Error: IPCKEY in \$SVCENV_CNFG invalid"
        return 1
    fi

    running=$(process_is_running $bin)
    if (( ! running ))
    then
        echo "$bin has already stopped... skip"
        return 0
    fi

    local addr="/tmp/agent_monitor_${ipckey}"
    local respond=$(echo "SHUTDOWN" | nc -U $addr)
    if [[ -z $respond ]]
    then
        echo "Error: down $bin no response"
        return 1
    fi
    echo ${respond%.*}
    sleep 1

    running=$(process_is_running $bin)
    if (( ! running ))
    then
        echo "$bin stop succeed"
        return 0
    else
        echo "$bin stop fail"
        return 1
    fi
} #]]]

main $@
exit $?
